﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.MultiEditingInPathEditor.SetLocationsProperties.CUiSLPGridHostedCtrl
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using C1.Win.C1FlexGrid;
using System;
using System.Drawing;
using System.Windows.Forms;


namespace DnProcessSimulateCommands.MultiEditingInPathEditor.SetLocationsProperties;

public class CUiSLPGridHostedCtrl
{
  private C1.Win.C1FlexGrid.C1FlexGrid m_flex;
  private Control m_ctl;
  private Row m_row;
  private Column m_col;
  private bool m_allowTabStop;
  private CUiSLPGridHostedCtrl.CtrlPositionInCellStyle m_positionInCellStyle;

  public CUiSLPGridHostedCtrl(C1.Win.C1FlexGrid.C1FlexGrid flex, Row row, Column col)
  {
    this.m_flex = flex;
    this.m_row = row;
    this.m_col = col;
  }

  public Row Row => this.m_row;

  public Column Column => this.m_col;

  public int TabIndex => this.m_ctl.TabIndex;

  public bool AllowTabStop => this.m_allowTabStop;

  public CUiSLPGridHostedCtrl.CtrlPositionInCellStyle PositionInCellStyle
  {
    get => this.m_positionInCellStyle;
  }

  public bool Visible
  {
    get => this.m_ctl.Visible;
    set
    {
      if (this.m_ctl.Visible == value)
        return;
      this.m_ctl.Visible = value;
    }
  }

  public void AttachControl(
    Control ctl,
    int tabIndex,
    bool allowTabStop,
    CUiSLPGridHostedCtrl.CtrlPositionInCellStyle positionInCellStyle)
  {
    this.DetachControl();
    this.m_ctl = ctl;
    this.initCtrl(tabIndex, allowTabStop, positionInCellStyle);
    ((Control) this.m_flex).Controls.Add(this.m_ctl);
    this.registerToCtrlEvents();
  }

  public Control DetachControl()
  {
    Control ctl = this.m_ctl;
    if (this.m_ctl != null)
    {
      this.unregisterToCtrlEvents();
      ((Control) this.m_flex).Controls.Remove(this.m_ctl);
      this.m_ctl = (Control) null;
    }
    return ctl;
  }

  public bool UpdatePosition()
  {
    if (this.m_ctl == null)
      return false;
    int index1 = ((RowCol) this.m_row).Index;
    int index2 = ((RowCol) this.m_col).Index;
    if (index1 < 0 || index2 < 0)
      return false;
    Rectangle cellRect = ((C1FlexGridBase) this.m_flex).GetCellRect(index1, index2, false);
    if (cellRect.Width > 0 && cellRect.Height > 0 && cellRect.IntersectsWith(((Control) this.m_flex).ClientRectangle) && ((RowCol) this.m_row).Visible && ((RowCol) this.m_col).Visible)
      return this.UpdatePosition(cellRect);
    this.Visible = false;
    return true;
  }

  protected virtual bool UpdatePosition(Rectangle visibleCellRect)
  {
    bool flag = false;
    switch (this.m_positionInCellStyle)
    {
      case CUiSLPGridHostedCtrl.CtrlPositionInCellStyle.MiddleCenter:
        if (visibleCellRect.Width - this.m_ctl.Width < -1 || visibleCellRect.Height - this.m_ctl.Height < -1)
        {
          this.Visible = false;
          flag = true;
          break;
        }
        this.m_ctl.Bounds = new Rectangle(new Point(visibleCellRect.Left + (visibleCellRect.Width - this.m_ctl.Width) / 2, visibleCellRect.Top + (visibleCellRect.Height - this.m_ctl.Height) / 2), this.m_ctl.Size);
        this.Visible = true;
        flag = true;
        break;
    }
    return flag;
  }

  private void initCtrl(
    int tabIndex,
    bool allowTabStop,
    CUiSLPGridHostedCtrl.CtrlPositionInCellStyle positionInCellStyle)
  {
    this.m_allowTabStop = allowTabStop;
    this.m_positionInCellStyle = positionInCellStyle;
    this.m_ctl.TabIndex = tabIndex;
    if (this.m_allowTabStop)
      this.m_ctl.TabStop = this.m_ctl.Enabled;
    else
      this.m_ctl.TabStop = false;
  }

  private void registerToCtrlEvents()
  {
    if (this.m_ctl == null)
      return;
    this.m_ctl.EnabledChanged += new EventHandler(this.m_ctl_EnabledChanged);
    this.m_ctl.Click += new EventHandler(this.m_ctl_Click);
  }

  private void unregisterToCtrlEvents()
  {
    if (this.m_ctl == null)
      return;
    this.m_ctl.EnabledChanged -= new EventHandler(this.m_ctl_EnabledChanged);
    this.m_ctl.Click -= new EventHandler(this.m_ctl_Click);
  }

  public event EventHandler Click;

  private void m_ctl_EnabledChanged(object sender, EventArgs e)
  {
    if (!this.m_allowTabStop)
      return;
    this.m_ctl.TabStop = this.m_ctl.Enabled;
  }

  private void m_ctl_Click(object sender, EventArgs e)
  {
    if (this.Click == null)
      return;
    this.Click((object) this, e);
  }

  public enum CtrlPositionInCellStyle
  {
    Custom,
    MiddleCenter,
  }
}
